import 'package:flutter/material.dart';
import 'package:flutter_demo/getx/count_controller.dart';
import 'package:flutter_demo/getx/person_class.dart';
import 'package:get/get.dart';

class GetxObsPage extends StatefulWidget {
  const GetxObsPage({super.key});

  @override
  State<GetxObsPage> createState() => _GetxObsPageState();
}

class _GetxObsPageState extends State<GetxObsPage> {
  RxInt _count = 0.obs;
  final _items = RxList<String>(['zhangsan']);
  Person person = Person();
  Rx<Animal> animal = Animal(name: '小猫', age: 10).obs;

  // CountController countController = Get.put(CountController()); // 实例化controller
  CountController countController = Get.find();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Getx Obs"),
        backgroundColor: Colors.blue,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Obx(() => Text('$_count',
                style: Theme.of(context).textTheme.headlineLarge)),
            Obx(() {
              return Wrap(
                children: _items.map((element) => Text(element)).toList(),
              );
            }),
            Obx(() => Text('${person.username}')),
            Obx(() => Text(animal.value.name)),
            Obx(() => Text('${countController.count}',
                style: const TextStyle(color: Colors.blue, fontSize: 40))),
            ElevatedButton(
              onPressed: () {
                Get.toNamed('/login');
              },
              child: const Text('login'),
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton.small(
        child: const Icon(Icons.add),
        onPressed: () {
          _count++;
          _items.add('_element_');
          person.username.value = person.username.toUpperCase();

          /// 此处是对animal类进行了监听，所以改变animal类的某个属性后要对animal类进行赋值
          animal.value.name = '小狗';
          animal.value = animal.value;

          countController.inc();
        },
      ),
    );
  }
}
